layui.config({
    base: "js/"
}).use(['table', 'layer', 'form', 'jquery'], function () {
    var table = layui.table,
        form = layui.form,
        layer = parent.layer === undefined ? layui.layer : parent.layer,
        $ = layui.jquery;

    $.ajax({
        url: "../product/queryProductList",
        type: 'get',
        dataType: 'json',
        contentType: 'application/json;charset=utf-8',
        success: function (data) {
            $(".item_price").text(data.data[0].productPrice);
            $(".pro-image-front").attr("src", data.data[0].productImageEntity.image);
            $(".pro-image-back").attr("src", data.data[0].productImageEntity.image);
            $("#productName").html("<span>" + data.data[0].name + "</span>");
            $("#productsId").text(data.data[0].id);
            $("#productsId").hide();
        },
    });

    $("#exit").click(function () {
        $.ajax({
            url: "../users/exitLogin",
            type: 'get',
            success: function (data) {
                window.setInterval(function () {
                    location.reload();
                }, 1000);

            },
        });
    })


    /**
     * 监听提交
     */
    form.on('submit(formDemo)', function (data) {
        console.log(data)
        layer.confirm("确认要购买吗?", {
            title: "购买"
            , yes: function () {
                $.ajax({
                    url: "../finance/buyProducts",
                    type: 'post',
                    dataType: 'json',
                    //contentType: 'application/json;charset=utf-8',
                    data: {
                        aggregatePrice: $(".item_price").text(),
                        productsId: $("#productsId").text(),
                        productNum: 1
                    },
                    success: function (data) {
                        console.log(data);
                        layer.msg(data.msg + "..", {shift: 6, offset: 'rb'});
                    },
                    error: function () {
                        layer.msg("稍等再尝试" + "..", {shift: 6, offset: 'rb'});
                    }
                });
            }
        })

        return false;
    })

    /***
     *
     * 显示全部订单信息
     *
     */

    $("#queryOrderList").click(function () {
        $("#productsList").hide();

        table.render({
            elem: '#queryOrderListId'
            , url: '../finance/queryOrderList'
            , cols: [
                [
                    {type: 'checkbox', fixed: 'left', value: 'productId'}
                    , {
                    field: 'productImageEntity', title: '商品图片',
                    templet: function (data) {
                        return '<img class="layui-circle adminImage" width="26" height="26" src=' + data.productImageEntity.image + '>';
                    }
                }
                    , {field: 'name', title: '商品名称'}


                    , {
                    field: 'siteEntity', title: '手机号码',
                    templet: function (data) {
                        return '<span>' + data.siteEntity.phone + '</span>'
                    }
                }, {
                    field: 'siteEntity', title: '收货地址',
                    templet: function (data) {

                        return '<span>' + data.siteEntity.siteAddress + '</span>'
                    }
                }, {
                    field: 'financeEntity', title: '购买时间',
                    templet: function (data) {

                        return '<span>' + data.financeEntity.createTime + '</span>'
                    }
                }, {
                    field: 'financeEntity', title: '数量',
                    templet: function (data) {

                        return '<span>' + data.financeEntity.productNum + '</span>'
                    }
                }, {field: 'productPrice', title: '单价', sort: true},

                    {
                        field: 'financeEntity', title: '总价',
                        templet: function (data) {

                            return '<span>' + data.financeEntity.aggregatePrice + '</span>'
                        }
                    }
                    , {
                    fixed: 'right', title: '操作', toolbar: '#barDemo', width: 140
                }
                ]
            ]
            , limit: 20
            , limits: [10, 20, 25, 50, 100]
            , parseData: function (data) {
                /**
                 * 打印数据
                 *
                 */
                console.log(data);
                // layer.msg(data.msg + "..", {shift: 6, offset: 'rb'});
            }
            /**
             * 开启分页
             */
            , page: true
            , id: 'idTest'
        });

    })


})